<?php
class fFormElementSelectDate extends fFormElement {
	const DISPLAY_MOUNTH_WIDE = 'wide';
	const DISPLAY_MOUNTH_ABBREVIATED = 'abbreviated';
	const DISPLAY_MOUNTH_NUMBER = 'number';
	
	protected $_date;
	
	public function __construct($name, $date = null, $options = array()) {
		if ($date == null) {
			$this->_date = fDate::getDateFromTimeStamp();			
		}
		else if (is_string($date)) {
    		$date = (ctype_digit($date))? (int) $date: strtotime($date);
    		$this->_date = fDate::getDateFromTimeStamp($date);
    	}
    	else {
    		$this->_date = $date;
    	}
    	$this->_options['format'] = '%mounth% / %day% / %year';
    	$this->_options['display_mounth_mode'] = self::DISPLAY_MOUNTH_NUMBER;
    	
    	// build start and end year
    	$rangeOfYear = (isset($options['year_range']))? $options['year_range'] : 5;
    	
    	$options['start_year'] = (isset($options['start_year']))? $options['start_year'] : $this->_date['year'] - $rangeOfYear;
    	
    	if (!isset($options['end_year'])) {
    		$cYear = date('Y');    		
    		if ($this->_date['year'] < ($cYear - $rangeOfYear)) {
    			$options['end_year'] = $cYear;
    		}
    		else {
    			$options['end_year'] = $this->_date['year'] + $rangeOfYear;
    		}    		
    	}
    	
    	parent::__construct($name, $options);
	}
	
	/**
	 * Set Display Mounth Mode
	 *
	 * @param string $mode
	 */
	public function setDisplayMounthMode($mode = 'number') {
		$this->_options['display_mounth_mode'] = $mode;		
	}
	
	/**
	 * Render HTML string
	 *
	 * @return string
	 */
	public function render() {
		$date = array();
		$date['%day%'] = $this->renderDaySelect();
		$date['%mounth%'] = $this->renderMounthSelect();
		$date['%year'] = $this->renderYearSelect();
		return strtr($this->getOption('format'), $date);		
	}
	
	/**
	 * Render HTML tag Select of Day
	 *
	 * @return string
	 */
	protected function renderDaySelect() {		
		$html = '<select name="' .$this->_name.'[day]">';
		for($i = 1; $i<= 31; ++$i) {
			$html .= '<option value="' .$i .'"' .
					(($i == $this->_date['mday'])? ' selected="selected"': '') .'>' .
					sprintf('%02d', $i) .'</option>';
		}
		$html .= '</select>';
		return $html;
	}
	
	/**
	 * Reder HTML tag select for mounths
	 *
	 * @return string
	 */
	protected function renderMounthSelect() {
		$html = '<select name="' .$this->_name.'[mounth]">';
		$mounths = array();
		
		if ($this->_options['display_mounth_mode'] != self::DISPLAY_MOUNTH_NUMBER) {
			$mounths = array_values(fFactory::getUser()
										->getCulture()
										->getMonthNames($this->_options['display_mounth_mode']));			
		}
		else {
			for ($i = 1; $i<=12; ++$i) {
				$mounths[] = sprintf('%02d', $i);			
			}
		}
		for($i = 1; $i<= 12; ++$i) {
			$html .= '<option value="' .$i .'"' .
					(($i == $this->_date['mon'])? ' selected="selected"': '') .'>' .
					$mounths[$i-1] .'</option>';
		}
		$html .= '</select>';
		return $html;		
	}
	
	/**
	 * Render html tag select for year
	 *
	 * @return string
	 */
	protected function renderYearSelect() {
		$html = '<select name="' .$this->_name.'[year]">';
		$years = range($this->_options['start_year'], $this->_options['end_year']);
		for($i = sizeof($years) - 1; $i >= 0; --$i) {
			$html .= '<option value="' .$years[$i] .'"' .
					(($years[$i] == $this->_date['year'])? ' selected="selected"': '') .'>' .
					$years[$i] .'</option>';
		}
		$html .= '</select>';
		return $html;		
	}
}